package com.jl.obulibrary.servive;

import android.util.Log;

import com.jl.obulibrary.driver.RSUSerialDriver;
import com.jl.obulibrary.exceptions.OperationBySerialPortException;
import com.jl.obulibrary.utils.HexUtil;
import com.jxhc.rsudriver.ETC_CONST;
import com.jxhc.rsudriver.ICustomRsuDriver;
import com.jxhc.rsudriver.IRsuDriver;
import com.jxhc.rsudriver.RsuDriverFactory;
import com.jxhc.rsudriver.model.GetSecure_res;
import com.jxhc.rsudriver.model.INITIALISATION_res;
import com.jxhc.rsudriver.model.RSU_INIT_res;
import com.jxhc.rsudriver.model.RSU_Info_res;
import com.jxhc.rsudriver.model.TransferChannel_res;


public class OperationBySerialPort {
    static String TAG = "大智测试OperationBySerialPort";
    private IRsuDriver standardDriverInstance;
    private ICustomRsuDriver customDriverInstance;
    private long fd;

    //初始化
    public String initRsuTrans(String rsuIp, int power) {
        RsuDriverFactory.registerCommunicateDriver(new RSUSerialDriver());
        standardDriverInstance = RsuDriverFactory.getStandardDriverInstance();
        customDriverInstance = RsuDriverFactory.getCustomDriverInstance();
        fd = standardDriverInstance.RSU_Open(0, rsuIp, 0);
        //   resultTextView.append("打开串口,句柄:"+fd+"\n");

        RSU_INIT_res rsu_INIT = standardDriverInstance.RSU_INIT(fd, String.format("%08X", System.currentTimeMillis() / 1000),
                10, 0x30, power, 0, 1000);
        //  resultTextView.append("初始化模块:"+rsu_INIT +"\n");
        customDriverInstance.setPowAfterCal(fd, (short) power, 2000);

        return "打开串口,句柄:" + fd + "</br>初始化模块:" + rsu_INIT;
    }

    public INITIALISATION_res JL_CustomBst() throws OperationBySerialPortException {

        String strSend = "FFFFFFFF500391C003" + HexUtil.GetRand(3)
                + "53ACE1CD0001418329A01A0004002B00";
        Log.e(TAG, "BST原语:"+strSend);
        INITIALISATION_res vst = standardDriverInstance.INITIALISATION_CUSTOM(fd,strSend  , 2000);    //这个指令直接携带余额
        Log.e(TAG, "SDK返回的vst对象为:"+vst);
        if (vst.rtnValue == ETC_CONST.RTN_DEVICE_NOT_OPEN || vst.rtnValue == ETC_CONST.RTN_DEVICE_NO_RESPONSE) {
            throw new OperationBySerialPortException("模块通信错误");
        } else if (vst.rtnValue != 0 || vst.returnStatus != 0) {
            throw new OperationBySerialPortException("未读到OBU");
        }
        return vst;

    }

    public INITIALISATION_res JL_BstBySn(String SN) throws OperationBySerialPortException {
        String time = String.format("%08X", System.currentTimeMillis() / 1000);
        String beaconId = time;
        INITIALISATION_res vst = standardDriverInstance.INITIALISATION(fd, beaconId, time, 0, 1, "418729a01a0004002b", 0, 2000);    //这个指令直接携带余额
        // resultTextView.append("BST:"+ vst +"\n");
        if (vst.rtnValue == ETC_CONST.RTN_DEVICE_NOT_OPEN || vst.rtnValue == ETC_CONST.RTN_DEVICE_NO_RESPONSE) {
            throw new OperationBySerialPortException("模块通信错误");
        } else if (vst.rtnValue != 0 || vst.returnStatus != 0) {
            throw new OperationBySerialPortException("未读到OBU");
        }
        return vst;
    }

    public INITIALISATION_res JL_BstByMac(String mac) throws OperationBySerialPortException {
        String time = String.format("%08X", System.currentTimeMillis() / 1000);
        String beaconId = time;
        INITIALISATION_res vst = standardDriverInstance.INITIALISATION(fd, beaconId, time, 0, 1, "418729a01a0004002b", 0, 2000);    //这个指令直接携带余额
        // resultTextView.append("BST:"+ vst +"\n");
        if (vst.rtnValue == ETC_CONST.RTN_DEVICE_NOT_OPEN || vst.rtnValue == ETC_CONST.RTN_DEVICE_NO_RESPONSE) {
            throw new OperationBySerialPortException("模块通信错误");
        } else if (vst.rtnValue != 0 || vst.returnStatus != 0) {
            throw new OperationBySerialPortException("未读到OBU");
        }
        return vst;
    }


    public GetSecure_res JL_GetSecure(int length,String strRandom)   {



        GetSecure_res gs = standardDriverInstance.GetSecure(fd, 0, 1, 1, "0000000000000000", 1, 1, 0, length, strRandom, 0, 0, 2000);


        return gs;
    }
                                        // channalID=2 cos=00A40000023F00
    public TransferChannel_res JL_TransferChannel(int channalID, String cos) throws OperationBySerialPortException {

        String coslenString=    getLengthAsHex(cos);
        String cosall = coslenString + cos;

//        Log.e(TAG, "通道号:"+channalID+",通道数据"+cosall);

        TransferChannel_res res = standardDriverInstance.TransferChannel(fd, 1, 1, channalID, 1, cosall, 2000);


        return res;
    }
    public TransferChannel_res JL_TransferChannel_CUSTOM(String apdu) throws OperationBySerialPortException {



        TransferChannel_res res = standardDriverInstance.TransferChannel_CUSTOM(fd, apdu, 2000);


        return res;
    }



    public static String getLengthAsHex(String str) {
        int length = str.length();
        String hex = Integer.toHexString(length/2).toUpperCase();
        // 补零到偶数位，确保每个字节两位
        if (hex.length() % 2 != 0) {
            hex = "0" + hex;
        }
        return hex;
    }
    public void JL_SetMMI() throws OperationBySerialPortException {
        //选择IC卡DF01文件

        standardDriverInstance.SetMMI(fd, 1, 1, 0, 1000);

    }

    public void JL_EVENT_REPORT() throws OperationBySerialPortException {
        //选择IC卡DF01文件

        standardDriverInstance.EVENT_REPORT(fd, 1, 1, 1, 1000);

    }

    public String getSecure() throws OperationBySerialPortException {
        String time = String.format("%08X", System.currentTimeMillis() / 1000);
        String beaconId = time;
        INITIALISATION_res vst = standardDriverInstance.INITIALISATION(fd, beaconId, time, 0, 1, "418729a01a0004002b", 0, 2000);    //这个指令直接携带余额
        // resultTextView.append("BST:"+ vst +"\n");
        if (vst.rtnValue == ETC_CONST.RTN_DEVICE_NOT_OPEN || vst.rtnValue == ETC_CONST.RTN_DEVICE_NO_RESPONSE) {
            throw new OperationBySerialPortException("模块通信错误");
        } else if (vst.rtnValue != 0 || vst.returnStatus != 0) {
            throw new OperationBySerialPortException("未读到OBU");
        }
        int length = 16;
        GetSecure_res gs = standardDriverInstance.GetSecure(fd, 0, 1, 1, "0000000000000000", 1, 1, 0, length, "ed506a24c9ad1d92", 0, 0, 2000);
        //  resultTextView.append("读取车辆信息密文:"+gs+"\n");
        if (gs.rtnValue == ETC_CONST.RTN_DEVICE_NO_RESPONSE || gs.file == null) {
            //      resultTextView.append("模块通信错误\n");
            throw new OperationBySerialPortException("模块通信错误");
        }

        //释放链路，响一声
        standardDriverInstance.SetMMI(fd, 1, 1, 0, 1000);
        standardDriverInstance.EVENT_REPORT(fd, 1, 1, 1, 1000);


        return "VST:" + vst + "</br>读取车辆信息密文" + gs;
    }


    public String readIcc() {
        String time = String.format("%08X", System.currentTimeMillis() / 1000);
        String beaconId = time;
        INITIALISATION_res vst = standardDriverInstance.INITIALISATION(fd, beaconId, time, 0, 1, "418729a01a0004002b", 0, 2000);    //这个指令直接携带余额
        // resultTextView.append("BST:"+ vst +"\n");

        //选择IC卡DF01文件
        TransferChannel_res res = standardDriverInstance.TransferChannel(fd, 1, 1, 1, 1, "0700A40000021001", 2000);
        // resultTextView.append("选择IC卡DF01文件:"+res+"\n");

        //读取IC卡余额
        TransferChannel_res res2 = standardDriverInstance.TransferChannel(fd, 1, 1, 1, 1, "05805C000204", 2000);
        //  resultTextView.append("读取IC卡余额:"+res+"\n");

        //读过站信息
        TransferChannel_res res3 = standardDriverInstance.TransferChannel(fd, 1, 1, 1, 1, "0500B201cc2b", 2000);
        // resultTextView.append("读取过站信息文件:"+res+"\n");

        //释放链路，响一声
        standardDriverInstance.SetMMI(fd, 1, 1, 0, 1000);
        standardDriverInstance.EVENT_REPORT(fd, 1, 1, 1, 1000);


        return "VST:" + vst + "</br>选择IC卡DF01文件:" + res + "</br>读取IC卡余额:" + res2 + "</br>读取过站信息文件:" + res3;
    }

    //关闭
    public String closeRsu() {
        standardDriverInstance.RSU_Close(fd);
        fd = -1;
        return "成功关闭串口";
    }


    private static OperationBySerialPort instance;

    public static OperationBySerialPort getInstance() {
        if (instance == null) {
            instance = new OperationBySerialPort();
        }
        return instance;
    }
    public String getRsuVerison() throws Exception {

        RSU_Info_res res4=  standardDriverInstance.RSU_VERSION(fd,1000);
        return  res4.rsuInfo;
    }
}
